
/*

showPages v1.1
=================================

Infomation
----------------------
Author : Lapuasi
E-Mail : lapuasi@gmail.com
Web : http://www.lapuasi.com
Date : 2005-11-17


Example
----------------------
var pg = new showPages('pg');
pg.pageCount = 12; //定义总页数(必要)
pg.argName = 'p';    //定义参数名(可选,缺省为page)
pg.printHtml();        //显示页数


Supported in Internet Explorer, Mozilla Firefox
*/
$(function(){
	var sessionStorage = window.sessionStorage;
	var loginName = sessionStorage.getItem("loginName");
	$("#user_name").html("您好，"+loginName+"，欢迎登陆!");
});

function showPages(name,page,pageCount,count) { //初始化属性
	this.name = name;      //对象名称
	this.page = page;         //当前页数
	this.pageCount = pageCount;    //总页数
	this.argName = 'page'; //参数名
	this.showTimes = 1;    //打印次数
	this.count = count; //总条数
}

showPages.prototype.getPage = function(){ //丛url获得当前页数,如果变量重复只获取最后一个
	//var args = location.search;
	//var reg = new RegExp('[\?&]?' + this.argName + '=([^&]*)[&$]?', 'gi');
	//var chk = args.match(reg);
	//this.page = RegExp.$1;
	//this.page = ;
}
showPages.prototype.checkPages = function(){ //进行当前页数和总页数的验证
	if (isNaN(parseInt(this.page))) this.page = 1;
	if (isNaN(parseInt(this.pageCount))) this.pageCount = 1;
	this.page = parseInt(this.page);
	this.pageCount = parseInt(this.pageCount);
}
showPages.prototype.createHtml = function(mode){ //生成html代码
	var strHtml = '', prevPage = this.page - 1, nextPage = this.page + 1;
	if (mode == '' || typeof(mode) == 'undefined') mode = 0;
	switch (mode) {
		
		case 2 : //模式2 (前后缩略,页数,首页,前页,后页,尾页)
			strHtml+='<div>';
			if (prevPage < 1) {
				strHtml+='<a  class="button">首页</a>&nbsp;';
				strHtml+='<a  class="button">上一页</a>&nbsp;';
			} else {
				strHtml+='<a href="javascript:' + this.name + '.toPage(1);" class="button">首页</a>&nbsp;'
				strHtml+='<a href="javascript:'+ this.name + '.toPage(' + prevPage + ');" class="button">上一页</a>&nbsp;';
			}
			if (this.page != 1) strHtml += '<a class="button" href="javascript:' + this.name + '.toPage(1);">1</a>&nbsp;';
			if (this.page >= 5) strHtml += '<span class="button">...</span>&nbsp;';
			if (this.pageCount > this.page + 2) {
				var endPage = this.page + 2;
			} else {
				var endPage = this.pageCount;
			}
			for (var i = this.page - 2; i <= endPage; i++) {
				if (i > 0) {
					if (i == this.page) {
						strHtml += '<span class="button" style="background-color:#FFFFFF;" title="Page ' + i + '">' + i + '</span>&nbsp;';
					} else {
						if (i != 1 && i != this.pageCount) {
							strHtml += '<a class="button" href="javascript:' + this.name + '.toPage(' + i + ');">' + i + '</a>&nbsp;';
						}
					}
				}
			}
			if (this.page + 3 < this.pageCount) strHtml += '<span class="button" >...</span>&nbsp;';
			if (this.page != this.pageCount) strHtml += '<a class="button"  href="javascript:' + this.name + '.toPage(' + this.pageCount + ');">' + this.pageCount + '</a>&nbsp;';
			if (nextPage > this.pageCount) {
				strHtml +='<a class="button">下一页</a>&nbsp;';
				strHtml +='<a class="button">尾页</a>';
			} else {
				strHtml += '<a class="button" href="javascript:' + this.name + '.toPage(' + nextPage + ');">下一页</a>';
				strHtml +=' <a class="button" href="javascript:' + this.name + '.toPage(' + this.pageCount + ');">尾页</a>';
			}
			strHtml += '<span style="padding-left:20px;">共'+this.count+'条记录</span>';
			strHtml += '</div>';
			break;
			
		default :
			strHtml = 'Javascript showPage Error: not find mode ' + mode;
			break;
	}
	return strHtml;
}
//showPages.prototype.createUrl = function (page) { //生成页面跳转url
//	if (isNaN(parseInt(page))) page = 1;
//	if (page < 1) page = 1;
//	if (page > this.pageCount) page = this.pageCount;
//	var url = location.protocol + '//' + location.host + location.pathname;
//	return url+"?page="+page;
//}
showPages.prototype.toPage = function(page){ //页面跳转
	var turnTo = 1;
	if (typeof(page) == 'object') {
		turnTo = page.options[page.selectedIndex].value;
	} else {
		turnTo = page;
	}
	self.location.href = this.createUrl(turnTo);
	//$("#main").load(this.createUrl(turnTo));
}
showPages.prototype.printHtml = function(mode){ //显示html代码
	this.checkPages();
	this.showTimes += 1;
	var ht=this.createHtml(mode);
	$("#pages").empty();
	$("#pages").append($(ht));
	
}
showPages.prototype.formatInputPage = function(e){ //限定输入页数格式
	var ie = navigator.appName=="Microsoft Internet Explorer"?true:false;
	if(!ie) var key = e.which;
	else var key = event.keyCode;
	if (key == 8 || key == 46 || (key >= 48 && key <= 57)) return true;
	return false;
}